<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Module
 *
 * @author gemeijie
 */
class Libs_Admin_Category {

    protected static $_dbAdmin;

    /**
     * 数据库链接
     */
    public static function loadDb() {
        self::$_dbAdmin = Db_Admin::instance();
    }

    /*
     * 添加新类
     */
    public static function addCategory(array $param){
        $classid = !empty ($param['classid']) ? $param['classid'] : 0;
        $subclassid = !empty ($param['subclassid']) ? $param['subclassid'] : 0;
        $module_type = !empty ($param['module_type']) ? $param['module_type'] : 0;
        $name = !empty ($param['name']) ? $param['name'] : '';

        if($name == '' || $module_type == 0){
            return false;
        }

        self::loadDb();

        if($classid == 0){
            $class_sql = "Select max(classid) from category_list";
            $last_classid = self::$_dbAdmin->get_var($class_sql);
            $last_classid = !empty ($last_classid) ? $last_classid+1 : 1;

            $sql1 = "insert into category_list(classid, subclassid, name, module_type, isdel) values ('$last_classid',0,'$name',$module_type,0)";
            self::$_dbAdmin->query($sql1);
            return true;
        }

        $subclass_sql = "Select max(subclassid) from category_list";
        $last_subclassid = self::$_dbAdmin->get_var($subclass_sql);
        $last_subclassid = !empty ($last_subclassid) ? $last_subclassid+1 : 1;

        $sql2 = "insert into category_list(classid, subclassid, name, module_type, isdel) values ($classid,$last_subclassid,'$name',$module_type,0)";
        self::$_dbAdmin->query($sql2);
        return true;
    }


    /*
     *获取类列表
     * type:0,获取所有数据；1获取类数据，2，获取子类数据 3、该模块所有数据
     */
    public static function getCategoryList(array $param){
        $module_type = !empty ($param['module_type']) ? $param['module_type'] : 0;
        $classid = !empty($param['classid']) ? $param['classid'] : 0;
        $subclassid = !empty($param['subclassid']) ? $param['subclassid'] : 0;
        $page = !empty($param['page']) ? $param['page'] : 1;
        $pagesize = !empty($param['pagesize']) ? $param['pagesize']: 15;
        $startnum = ($page - 1) * $pagesize;
        $type = !empty($param['type']) ? $param['type'] : 0;


        self::loadDb();

        $sql = "Select * from category_list where isdel=0";

        switch ($type){
            #获取类别
            case 1:
                if($module_type == 0){
                   return false;
                }
                if($classid){
                    $sql .= " and module_type=$module_type and classid in ({$classid}) and subclassid=0";
                }else{
                    $sql .= " and module_type=$module_type and classid!=0 and subclassid=0";
                }
                break;
            #获取子类
            case 2:
                if( $module_type == 0){
                    return false;
                }
                if($classid == 0){
                    $sql .= " and module_type=$module_type and classid!=0 and subclassid!=0";
                }else{
                    $sql .= " and module_type=$module_type and classid=$classid and subclassid!=0";
                }
                if($subclassid){
                    $sql .= " and subclassid in ({$subclassid}) ";
                }
                break;
           #该模块所有数据
           case 3:
                if( $module_type == 0){
                    return false;
                }
                $sql .= " and module_type=$module_type ";
                break;
            # 获取所有数据
            default :
        }

        $sql .= " order by orderby desc , id asc limit $startnum, $pagesize";
        $list = self::$_dbAdmin->get_results($sql);
        //var_dump($list);exit;
        return $list;
    }


    /*
     *获取类列表
     * type:0,获取所有数据；1获取类数据，2，获取子类数据
     */
    public static function getCategoryCount(array $param){
        $module_type = !empty ($param['module_type']) ? $param['module_type'] : 0;
        $classid = !empty($param['classid']) ? $param['classid'] : 0;
        $subclassid = !empty($param['subclassid']) ? $param['subclassid'] : 0;
        $type = !empty($param['type']) ? $param['type'] : 0;


        self::loadDb();

        $sql = "Select count('X') from category_list where isdel=0";

        switch ($type){
            #获取类别
            case 1:
                if($module_type == 0){
                   return false;
                }
                $sql .= " and module_type=$module_type and classid!=0 and subclassid=0";
                break;
            #获取子类
            case 2:
                if($classid == 0 || $module_type == 0){
                    return false;
                }
                $sql .= " and module_type=$module_type and classid=$classid and subclassid!=0";
                break;
           case 3:
                if( $module_type == 0){
                    return false;
                }
                $sql .= " and module_type=$module_type ";
                break;
            # 获取所有数据
            default :
        }
        //echo $sql;exit;
        $count = self::$_dbAdmin->get_var($sql);
        return $count;
    }

    /*
     * 删除模块类
     */
    public static function delCategory(array $param){
        $id = !empty ($param['id']) ? $param['id'] : 0;

        if($id == 0){
            return false;
        }

        self::loadDb();

        $sql = "update category_list set isdel=1 where id=$id";
        self::$_dbAdmin->query($sql);
    }

}

?>
